# 怎样在一个序列上面保持元素顺序的同时消除重复的值

# 当元素为hashable的时候才管用
def dedupe_hashable(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)

# 如果元素不可hash（比如dict类型）的序列中重复元素
def dedupe(items, key=None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)

def demo_1():
    args = [1,5,2,1,9,1,5,10]
    l = list(dedupe_hashable(args))
    print(l)

def demo_2():
    args = [{'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]
    # 如果x,y存在重复，则去重
    l1 = list(dedupe(args, key = lambda d: (d['x'],d['y'])))
    # 如果x存在重复，则去重
    l2 = list(dedupe(args, key = lambda d: d['x']))
    print(l1)
    print(l2)

if __name__ == '__main__':
    demo_1()
    demo_2()